package com.scu.bank.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scu.bank.model.dao.ScienceResultDao;
import com.scu.bank.model.example.ScienceResultExample;
import com.scu.bank.model.pojo.ResearchProject;
import com.scu.bank.model.pojo.ScienceDynamic;
import com.scu.bank.model.pojo.ScienceResult;
import com.scu.bank.model.base.BaseServiceImpl;
import com.scu.bank.model.pojo.UserInfo;
import com.scu.bank.service.ResearchProjectService;
import com.scu.bank.service.ScienceResultService;
import com.scu.bank.util.ListUtils;
import com.scu.bank.util.StringGenerateUtil;
import com.scu.bank.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by pmeng on 2021/01/05.
 */
@Service
@Transactional
public class ScienceResultServiceImpl extends BaseServiceImpl<ScienceResult,ScienceResultDao> implements ScienceResultService {
    @Autowired
    private ScienceResultDao scienceResultDao;
    @Autowired
    private ResearchProjectService researchProjectService;

    @Override
    public ScienceResultDao getDao() {
        return scienceResultDao;
    }

    @Override
    public PageInfo<ScienceResult> list(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        ScienceResultExample example = new ScienceResultExample();
        example.createCriteria().andIsDelEqualTo(false);
        PageInfo pageInfo = new PageInfo(scienceResultDao.selectByExample(example));

        List<ScienceResult> list = pageInfo.getList();

        if (!ListUtils.isEmpty(list)){
            list.forEach((temp)->{
                if ("1".equals(temp.getType())){//1代表科研项目类成果
                    ResearchProject researchProject = researchProjectService.getById(temp.getResId());
                    temp.setScienceResultProjectName(researchProject == null ? "不存在的数据" : researchProject.getProName());
                }
            });
        }
        pageInfo.setList(list);

        return pageInfo;
    }

    @Override
    public void addScienceResult(ScienceResult scienceResult) {
        scienceResult.setUserId(WebUtil.getCurrentUserId());
        scienceResult.setType("1");
        scienceResult.setStatus("0");
        save(scienceResult);
    }
}
